package com.ncu.strategy.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.ncu.Enum.LoginTypeEnum;
import com.ncu.dto.SocialTokenDTO;
import com.ncu.dto.SocialUserInfoDTO;
import com.ncu.strategy.config.WxConfigProperties;
import com.ncu.strategy.mode.AbstractSocialLoginStrategyImpl;
import com.ncu.vo.WxLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service("wxLoginStrategyImpl")
public class WxLoginStrategyImpl extends AbstractSocialLoginStrategyImpl {


    @Autowired
    WxConfigProperties wxConfigProperties;
    @Override
    public SocialTokenDTO getSocialToken(String data) {
        WxLoginVO wxLoginVO = JSON.parseObject(data, WxLoginVO.class);

        //可以单独传入http参数，这样参数会自动做URL编码，拼接在URL中
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", wxConfigProperties.getAppId());
        paramMap.put("secret", wxConfigProperties.getAppSecret());
        paramMap.put("js_code", wxLoginVO.getCode());
        paramMap.put("grant_type","authorization_code");
        String result= HttpUtil.get(wxConfigProperties.getUrl(), paramMap);
        SocialTokenDTO socialTokenDTO = JSON.parseObject(result, SocialTokenDTO.class);
        socialTokenDTO.setLoginType(LoginTypeEnum.WX.getType());
        System.out.println(result);
        System.out.println(socialTokenDTO);
        return socialTokenDTO;
    }

    @Override
    public SocialUserInfoDTO getSocialUserInfo(SocialTokenDTO socialTokenDTO) {
        SocialUserInfoDTO socialUserInfoDTO = new SocialUserInfoDTO();
        socialUserInfoDTO.setNickname("默认用户");
        socialUserInfoDTO.setAvatar("http://localhost:8088/foodPricutre/f80a0659b31ede626eac3f627a9bb40f.jpg");
        return socialUserInfoDTO;
    }
}
